{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray",
   "id": "aa36a46331e8f2f3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## ndarray 特性\n",
   "id": "f9280ca724f8cbe7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 多维性",
   "id": "de7a1fa98fdb6ac1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T03:25:02.647100Z",
     "start_time": "2025-09-15T03:25:02.562592Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "a44d7838e5dd6c71",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T03:25:02.667950Z",
     "start_time": "2025-09-15T03:25:02.661886Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array(5)\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim)"
   ],
   "id": "760af927c3645c29",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "arr的维度： 0\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T03:25:12.794980Z",
     "start_time": "2025-09-15T03:25:12.789527Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2,3])\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim)"
   ],
   "id": "998bbe241c60ebd9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "arr的维度： 1\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim)"
   ],
   "id": "cff18c016be1df11",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 同质性(类型强制转换)",
   "id": "b1f2e89e77683b7e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1,'...'])\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim)"
   ],
   "id": "6b466fa536294de8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2.5])#'.'表示浮点，小数部分只有0时会省去\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim)"
   ],
   "id": "fb03333f1254db7e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## ndarray 属性",
   "id": "f7706e594118b3b0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### shape：数组的行数和列数 --arr.shape",
   "id": "88ad3949f9c1ec9a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.shape:',arr.shape)"
   ],
   "id": "5efe6d1a93a21bce",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### ndim：维度 --arr.ndim",
   "id": "dac8a065196feea6"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.ndim:',arr.ndim)"
   ],
   "id": "7eb991a61cbfa30",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### size：元素总个数 --arr.size",
   "id": "a4263077a9c42482"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.size:',arr.size)"
   ],
   "id": "dcb78213619bb79d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### dtype：元素类型 --arr.dtype",
   "id": "67d3d4d6e15cbee2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.dtype:',arr.dtype)#后面的数字表示占用的空间大小"
   ],
   "id": "482206733b1ec20d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### T: 转置 --arr.T",
   "id": "3875ad22e06cc80e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T03:26:02.623089Z",
     "start_time": "2025-09-15T03:26:02.618103Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.T:',arr.T)"
   ],
   "id": "452e9d5dd7fb602",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr.T: [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### itemsize：单个元素占用的内存字节数 --arr.itemsize",
   "id": "90706e4a88080385"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.itemsize:',arr.itemsize)"
   ],
   "id": "8cba2e10aded59a9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### nbytes: 数组总内存占用量 --arr.nbytes",
   "id": "9d38e50bb4c0387c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.nbytes:',arr.nbytes)"
   ],
   "id": "147bf95228e93188",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### flags：内存存储方式（是否连续存储--高级优化）--arr.flag",
   "id": "f1bbd87d042ea1c9"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print('arr.flags:',arr.flags)"
   ],
   "id": "56c791c6bc9497e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## ndarray 创建",
   "id": "f53f85bea3fa6a74"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "fae43c0415f546ec",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "arr1 = np.array([1,2,3])",
   "id": "d1a2dd3e12bd3c5f",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "list2 = [1,2,3]\n",
    "arr2 = np.array(list2)\n",
    "print(arr2)\n",
    "\n",
    "list3 = [1, 2, 3]\n",
    "arr3 = np.array(list3,dtype = np.float64)#强制转换\n",
    "print(arr3)"
   ],
   "id": "f105d3ad16a8ebc7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr4 = np.copy(arr2)\n",
    "print(arr4)"
   ],
   "id": "96e6c1086803015",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#预定义\n",
    "#全0\n",
    "arr5 = np.zeros((2,3))\n",
    "arr_5 = np.zeros_like(arr5)\n",
    "print(arr5)\n",
    "print(arr_5)\n",
    "print('\\n')\n",
    "#全1\n",
    "arr6 = np.ones((2,3))\n",
    "arr_6 = np.ones_like(arr6)\n",
    "print(arr6)\n",
    "print(arr_6)\n",
    "#未初始化的 效率高\n",
    "arr7 = np.empty((2,3))\n",
    "arr_7 = np.empty_like(arr7)\n",
    "print(arr7)\n",
    "print(arr_7)\n",
    "#固定值\n",
    "arr8 = np.full((2,3),324)\n",
    "arr_8 = np.full((2,3),314)\n",
    "print(arr8)\n",
    "print(arr_8)"
   ],
   "id": "8338175e921f84c3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 等差数列\n",
    "arr9 = np.arange(2,10,2)\n",
    "print(arr9)"
   ],
   "id": "c7c73604ce21b744",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#等间隔数列\n",
    "arr10 = np.linspace(0,4,5)\n",
    "print(arr10)"
   ],
   "id": "7d6f274f9c45a51d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#对数间隔数列，把范围分段然后作为底数的指数运算\n",
    "arr11 = np.logspace(0,4,3,base = 2)#第四个参数为底数，默认为10\n",
    "print(arr11)"
   ],
   "id": "6d6ac4eb2671ed10",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 特殊矩阵的生成",
   "id": "2565619dcb0efced"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 单位矩阵",
   "id": "45eef1d48fc301b1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr11 = np.eye(4,dtype = int)\n",
    "print(arr11)\n",
    "arr12 = np.eye(4,3,dtype = int)\n",
    "print(arr12)"
   ],
   "id": "59c33a8f8c1600ea",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 对角矩阵",
   "id": "baa590099b035fa1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#主对角线非0，其他数字为0\n",
    "arr13 = np.diag([1,3,5,7,9])\n",
    "print(arr13)"
   ],
   "id": "1fa8e857fc062b94",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 随机数组",
   "id": "7a64f8336bc6b856"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#生成0到1之间的随机浮点数（均匀分部，概率相同,每次运行生成不同的随机数）\n",
    "arr13 = np.random.rand(2,3)# 参数表示形状\n",
    "print(arr13)"
   ],
   "id": "df9759e625b58a8b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#生成指定范围内的随机浮点数\n",
    "arr14 = np.random.uniform(0,10,(2,3))\n",
    "print(arr14)"
   ],
   "id": "9dbbb4d62354a924",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#生成指定范围内的随机整数\n",
    "arr15 = np.random.randint(0,100,(1,30))\n",
    "print(arr15)"
   ],
   "id": "74f734974aa7e224",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#正态分布\n",
    "arr16 = np.random.randn(2,3)#范围从-3到3\n",
    "print(arr16)"
   ],
   "id": "f7329b0176ed46cd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#种子\n",
    "np.random.seed(100)#仅作配置，让随机数运行时不改变\n",
    "arr17 = np.random.randint(1,10,(2,5))\n",
    "print(arr17)"
   ],
   "id": "735b54bcef3b4e10",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## ndarray 数据类型\n",
    "#### bool\n",
    "#### int unit（8bit 16bit 32bit 64bit）\n",
    "#### float（16bit 32bit 64bit）\n",
    "#### complex（复数 64bit 128bit）"
   ],
   "id": "f4f965392a9d6301"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 索引与切片",
   "id": "ca9e4d48165ceefa"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "np.random.seed(100)\n",
    "#布尔索引\n",
    "arr = np.random.randint(1,100,(1,20))\n",
    "print(arr)\n",
    "print(arr[arr>10])\n",
    "print(arr[(arr>10)&(arr<50)])"
   ],
   "id": "378d42078f312eca",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "np.random.seed(100)\n",
    "#布尔索引\n",
    "arr = np.random.randint(1,100,(3,10))\n",
    "#二维数组的切片\n",
    "print(arr[:,:])\n",
    "print(arr[1,5:])\n",
    "#二维数组布尔索引\n",
    "print(arr[arr>50])\n",
    "print(arr[1][arr[1]<50])"
   ],
   "id": "a21e5ed593f15d1e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#列索引\n",
    "print(arr[:,2])#第三列"
   ],
   "id": "2078e888cd35ce5b",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## ndarray 运算",
   "id": "7223116ac9da2fc7"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "#算术运算\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([[4],[5],[6]])\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a@b)#矩阵的乘法\n",
    "print(a**2)\n",
    "print(a+1)\n",
    "print(\"---------------------------\")\n",
    "c = [1,2,3]\n",
    "d = [4,5,6]\n",
    "print(c+d)"
   ],
   "id": "d49298272a19b925",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 广播机制\n",
    "# 同一个维度，n*1 ，1*n\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([[4],[5],[6]])\n",
    "print(a+b)"
   ],
   "id": "62085e9ccbadffb2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "m## numpy 函数",
   "id": "79e1bd4cfe81ab59"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 基本算术函数",
   "id": "fc75a7e22305d062"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 平方根\n",
    "print(np.sqrt([1,2,4]))\n",
    "# 指数 e^()\n",
    "print(np.exp(1))\n",
    "# 自然对数 ln()\n",
    "print(np.log(2.71))\n",
    "# 三角函数\n",
    "print(np.sin(np.pi))\n",
    "print(np.cos(np.pi))"
   ],
   "id": "51528f0f3574334",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 统计函数",
   "id": "11d6ea4307cbe8b8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,20,8)\n",
    "print(arr)"
   ],
   "id": "a6be0ebc494adcd0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#求和\n",
    "print(np.sum(arr))"
   ],
   "id": "6ef1aa0b6c619231",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#求平均值\n",
    "print(np.mean(arr))"
   ],
   "id": "7e26d436f0f6e22e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#求中位数，分位数\n",
    "print(np.median(arr))\n",
    "print(np.percentile(arr,25))"
   ],
   "id": "a39c4184014c39a6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 方差，标准差\n",
    "print(np.var(arr))\n",
    "print(np.std(arr))"
   ],
   "id": "ef824a9fc88d0270",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 最值及其索引\n",
    "print(np.max(arr),np.argmax(arr))\n",
    "print(np.min(arr),np.argmin(arr))"
   ],
   "id": "a275920d50c9b7ac",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 累计和，累计积\n",
    "print(np.cumsum(arr))#此项与前一项的和(前缀和)\n",
    "print(np.cumprod(arr))"
   ],
   "id": "dc8d45b24ad61e00",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 逻辑函数m",
   "id": "770999a09b55ee35"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 矩阵和矩阵的比较是按位比较\n",
    "# 是否大于\n",
    "print(np.greater(arr, 15))\n",
    "# 是否小于\n",
    "print(np.less(arr, 15))\n",
    "# 是否等于\n",
    "print(np.equal(arr, 15))"
   ],
   "id": "30f48050c389e3e0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 逻辑与\n",
    "print(np.logical_and(1,0))\n",
    "# 逻辑或\n",
    "print(np.logical_or(1,0))\n",
    "# 逻辑非\n",
    "print(np.logical_not(1))"
   ],
   "id": "c40e4a395adc3b99",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 检查元素是否至少有一个为真\n",
    "print(np.any([0,0,0,0,1]))\n",
    "# 检查是否全部元素为真\n",
    "print(np.all([0,0,0,0,1]))"
   ],
   "id": "9c42103e1dffb022",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 自定义条件 三目运算\n",
    "print(np.where(arr>15,arr,0))\n",
    "print(np.where(arr>15,'y','n'))"
   ],
   "id": "245ed7aa69b44b4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 排序 numpy里的不更改原始数组\n",
    "print(np.sort(arr))\n",
    "print(np.argsort(arr))\n",
    "print(arr)"
   ],
   "id": "28ff5d33a5876238",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 去重\n",
    "print(np.unique(arr))"
   ],
   "id": "1598b0a13666de21",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# ndarray的拼接\n",
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([4,5,6])\n",
    "print(arr1+arr2)\n",
    "print(np.concatenate((arr1,arr2)))"
   ],
   "id": "dc7e39fb835e73bd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 数组的分割\n",
    "print(np.split(arr,2))\n",
    "print(np.split(arr,[0,3,5]))"
   ],
   "id": "4fbcb02acb0541e2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### axis 轴",
   "id": "57ee7cb116d2aa16"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr4 = np.random.randint(0,10,(4,3))\n",
    "print(arr4)\n",
    "print(\"每列的最大值:\",np.max(arr4,axis = 0))\n",
    "print(\"每行的最大值:\",np.max(arr4,axis = 1))\n",
    "print(\"每列的平均值:\",np.mean(arr4,axis = 0))"
   ],
   "id": "549acf49b6b73eb3",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
